#include "Defs.h"
#include "Zone.h"


Zone_T::Zone_T
(
	const char * szName, 
	const char * szDefaultLogFilename, 
	const char * szDefaultRulesFile, 
	const char * szConfigFile, 
	bool isZoneToInclude
)
	:hasTargetAddr(false)
	,hasTargetPort(false)
	,hasTargetUrl(false)
	,rulesPtr(0)
{
	FillMemory(_szName, sizeof(_szName), 0);
	_snprintf(_szName, sizeof(_szName) - 1, szName);

	// 
	// Get TargetAddr option
	//

	FillMemory(szTargetAddr, sizeof(szTargetAddr), 0);
	GetPrivateProfileString(_szName, "TargetAddr", "", 
		szTargetAddr, sizeof(szTargetAddr) - 1, szConfigFile);

	hasTargetAddr = lstrlen(szTargetAddr) > 0;

	// 
	// Get TargetPort option
	//

	FillMemory(szTargetPort, sizeof(szTargetPort), 0);
	GetPrivateProfileString(_szName, "TargetPort", "", 
		szTargetPort, sizeof(szTargetPort) - 1, szConfigFile);

	hasTargetPort = lstrlen(szTargetPort) > 0;

	// 
	// Get TargetUrl option
	//

	FillMemory(szTargetUrl, sizeof(szTargetUrl), 0);
	GetPrivateProfileString(_szName, "TargetUrl", "", 
		szTargetUrl, sizeof(szTargetUrl) - 1, szConfigFile);

	hasTargetUrl = lstrlen(szTargetUrl) > 0;

	// 
	// Get RulesFile option
	//

	FillMemory(szRulesFile, sizeof(szRulesFile), 0);
	if( isZoneToInclude )
	{
		GetPrivateProfileString("Setup", "RulesFile", szDefaultRulesFile, 
			szRulesFile, sizeof(szRulesFile) - 1, szConfigFile);

		if( !String_T(szRulesFile).Equals(szDefaultRulesFile, false) )
		{
			char szData[MAX_PATH];
			FillMemory(szData, sizeof(szData), 0);
			RtlMoveMemory(szData, szConfigFile, String_T(szConfigFile).LastIndexOf("\\") + 1);
			lstrcat(szData, szRulesFile);

			FillMemory(szRulesFile, sizeof(szRulesFile), 0);
			_snprintf(szRulesFile, sizeof(szRulesFile) - 1, szData);
		}

		rulesPtr = Rules_T::Load(szDefaultLogFilename, szRulesFile);
	}
}

void Zone_T::ToString(char * szBuffer, unsigned int bufferSize)
{
	unsigned int bufferCursor = 0;

	_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
		"# ================ Zone <%s>\r\n", _szName);
	bufferCursor = lstrlen(szBuffer);

	// 
	// Dump TargetAddr option
	//

	if( hasTargetAddr )
	{
		_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
			"# TargetAddr=%s\r\n", szTargetAddr);
		bufferCursor = lstrlen(szBuffer);
	}

	// 
	// Dump TargetPort option
	//

	if( hasTargetPort )
	{
		_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
			"# TargetPort=%s\r\n", szTargetPort);
		bufferCursor = lstrlen(szBuffer);
	}

	// 
	// Dump TargetUrl option
	//

	if( hasTargetUrl )
	{
		_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
			"# TargetUrl=%s\r\n", szTargetUrl);
		bufferCursor = lstrlen(szBuffer);
	}

	// 
	// Dump RulesFile option
	//

	if( lstrlen(szRulesFile) > 0 )
	{
		_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
			"# RulesFile=%s\r\n", szRulesFile);
		bufferCursor = lstrlen(szBuffer);
	}

	// 
	// end
	//
	
	_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
		"# ================ end\r\n");
	bufferCursor = lstrlen(szBuffer);
}

bool Zone_T::IsValid(const char * szZoneName, const char * szConfigFile, bool isZoneToInclude)
{
	char szData[MAX_PATH];
	char szText[MAX_PATH];

	// 
	// check is RulesFile key is set
	// and the file exists
	//

	if( isZoneToInclude )
	{
		FillMemory(szText, sizeof(szText), 0);
		GetPrivateProfileString(szZoneName, "RulesFile", "", 
			szText, sizeof(szText) - 1, szConfigFile);

		if( lstrlen(szText) > 0 )
		{
			FillMemory(szData, sizeof(szData), 0);
			RtlMoveMemory(szData, szConfigFile, String_T(szConfigFile).LastIndexOf("\\") + 1);
			lstrcat(szData, szText);

			if( !File_T::Exists(szData) )
			{
				Trace_T::Write("Zone %s is ignored because the rules file %s cannot be accessed", szZoneName, szData);
				return false;
			}
		}
	}

	// 
	// check is TargetAddr key is set
	//

	FillMemory(szData, sizeof(szData), 0);
	GetPrivateProfileString(szZoneName, "TargetAddr", "", 
		szData, sizeof(szData) - 1, szConfigFile);

	if( lstrlen(szData) > 0 )
	{
		return true;
	}

	// 
	// check is TargetPort key is set
	//

	FillMemory(szData, sizeof(szData), 0);
	GetPrivateProfileString(szZoneName, "TargetPort", "", 
		szData, sizeof(szData) - 1, szConfigFile);

	if( lstrlen(szData) > 0 )
	{
		return true;
	}

	// 
	// check is TargetUrl key is set
	//

	FillMemory(szData, sizeof(szData), 0);
	GetPrivateProfileString(szZoneName, "TargetUrl", "", 
		szData, sizeof(szData) - 1, szConfigFile);

	if( lstrlen(szData) > 0 )
	{
		return true;
	}

	return false;
}